package com.example.springback.service;

import com.example.springback.domain.Employee;
import com.example.springback.domain.Enterprise;
import com.example.springback.exception.CustomException;
import com.example.springback.mapper.EnterpriseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EnterPriseService {
    @Resource
    EnterpriseMapper enterpriseMapper;
    public PageInfo<Enterprise> selectPage(Enterprise enterprise, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<Enterprise> enterprises=enterpriseMapper.selectAll(enterprise);
        return PageInfo.of(enterprises);
    }

    public void add(Enterprise enterprise) {
        enterpriseMapper.insert(enterprise);
    }

    public Enterprise update(Enterprise enterprise) {
        String username=enterprise.getUsername();
        Enterprise enterprise1=enterpriseMapper.selectByUsername(username);//通过账号查询
        //如果有相同账号的不同企业的数据，则不能修改
        if(enterprise1!=null){
            //获取查到的企业的税号
            String taxNumber=enterprise1.getTaxNumber();
            if (!enterprise.getTaxNumber().equals(taxNumber)) {
                throw new CustomException("500","账号已存在，不可修改");
            }
        }
        enterpriseMapper.updateByPrimaryKey(enterprise);
        return enterpriseMapper.selectByPrimaryKey(enterprise.getEnterpriseId());
    }

    public void delete(Integer enterpriseId) {
        enterpriseMapper.deleteByPrimaryKey(enterpriseId);
    }

    public void deleteBatch(List<Enterprise> enterprises) {
        for (Enterprise enterprise:enterprises){
            enterpriseMapper.deleteByPrimaryKey(enterprise.getEnterpriseId());
        }

    }

    public Enterprise login(Enterprise enterprise) {//企业登录
        String username=enterprise.getUsername();//企业编号
        Enterprise enterprise1=enterpriseMapper.selectByUsername(username);
        if (enterprise1==null){//没查到
            throw new CustomException("500","账号不存在");
        }
        //账号存在
        String password=enterprise.getPassword();
        if (!enterprise1.getPassword().equals(password)){//不匹配
            throw new CustomException("500","账号或密码错误");
        }
        return enterprise1;
    }

    public Enterprise register(Enterprise enterprise) {
        String taxno=enterprise.getTaxNumber();
        String username=enterprise.getUsername();
        Enterprise enterprise1=enterpriseMapper.selectByTax(taxno);
        if(enterprise1!=null){
            throw new CustomException("500","企业已存在");
        }
        Enterprise enterprise2=enterpriseMapper.selectByUsername(username);
        if(enterprise2!=null){
            throw new CustomException("500","账号已存在");
        }
        enterpriseMapper.insert(enterprise);
        return enterpriseMapper.selectByUsername(username);//把注册好的企业信息返回去，直接登录
    }

    public int countAll() {
        return enterpriseMapper.countAll();
    }
}
